package com.sociesc.findasmartphonespark.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sociesc.findasmartphonespark.model.AccessPoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by daniel.arthur on 26/12/2014.
 */
public class rwJson {
    public static String writeJson(ArrayList<AccessPoint> aps) {
        //create ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();

        //create OutputStream instance for json object array
        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        //configure Object mapper for pretty print
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);

        try {
            objectMapper.writeValue(out, aps);
        } catch (IOException e) {
            e.printStackTrace();
        }
        final byte[] json = out.toByteArray();

        //System.out.println(new String(json));

        return new String(json);
    }

    public static ArrayList<AccessPoint> readJson(String json) {
        //create ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();

        ArrayList<AccessPoint> arrayAp = null;
        try {
            arrayAp = objectMapper.readValue(json, new TypeReference<ArrayList<AccessPoint>>(){});
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*for (int i = 0; i < arrayAp.size(); i++) {
            System.out.println(
                    "Nome: " + arrayAp.get(i).getSSID() +
                    " Mac: " + arrayAp.get(i).getBSSID() +
                    " Sinal: " + arrayAp.get(i).getRSSI() +
                    " Sala: " + arrayAp.get(i).getSala());
        }*/

        return arrayAp;
    }
}
